import axios from "axios";
import moment from "moment-timezone";
window.moment = moment;
const baseInstance = axios.create({
    baseURL: 'http://8.138.95.62:9999/api/',
    timeout: 10000,
    retry: 3, //设置全局重试请求次数（最多重试几次请求）
    retryDelay: 1000, //设置全局请求间隔
});
//时区
const timezones={
    'UTC+13':'Pacific/Apia',
    'UTC+12':'Asia/Anadyr',
    'UTC+11':'Asia/Magadan',
    'UTC+10':'Asia/Vladivostok',
    'UTC+9':'Asia/Chita',
    'UTC+8':'Hongkong',
    'UC+7':'Asia/Bangkok',
    'UTC+6':'Asia/Bishkek',
    'UTC+5':'Asia/Almaty',
    'UTC+4':'Asia/Dubai',
    'UTC+3':'Africa/Asmara',
    'UTC+2':'Africa/Bujumbura',
    'UTC+1':'Africa/Algiers',
    'UTC+0':'Africa/Accra',
    'UTC-1':'Atlantic/Cape_Verde',
    'UTC-2':'America/Noronha',
    'UTC-3':'America/Araguaina',
    'UTC-4':'America/Anguilla',
    'UTC-5':'America/Bogota',
    'UTC-6':'America/Monterrey',
    'UTC-7':'America/Dawson',
    'UTC-8':'Pacific/Pitcairn',
    'UTC-9':'Pacific/Gambier',
    'UTC-10':'Pacific/Johnston',
};

export async function getAllGoods({pageNo,pageSize} = {pageNo:1,pageSize:9999}){
    let data = await baseInstance.get('v1/platform/selectAllGoods',{
        params:{
            'page_no':pageNo,
            'page_size': pageSize
        }
    });
    return data;
}
//获取1000数据
export async function getKLinefromGoods({goods,period,lineId}){
    let data = await baseInstance.get('v1/platform/selectFrontKline',{
        params:{
            goods,
            period,
            lineId:lineId || 0
        }
    });
    let LineList = data.data.data.lineData;
    let utcNumber = data.data.data.utc || 0;
    let zone = timezones[`UTC${utcNumber>=0?'+':''}${utcNumber}`];
    LineList = LineList.map(v => ({...v,timestamp: moment.tz(v.timestamp,zone).valueOf(), volume: v.vol }));
    LineList.sort((aLine,bLine) => (new Date(aLine.timestamp) > new Date(bLine.timestamp) ? 1 : -1) ); //按时间从小到大排序
    return LineList;
}
//获取1000 * n 数据
export async function getKLinefromGoodsfromN({goods,period,n}){
    let kLine = [];
    let zone = '';
    for(let i = 0; i < n; i++){
        let lineId = 0;
        if(i > 0){
            lineId = kLine[0].pkId;
        }
        let data = await baseInstance.get('v1/platform/selectFrontKline',{
            params:{
                goods,
                period,
                lineId
            }
        });
        let LineList = data.data.data.lineData;
        let utcNumber = data.data.data.utc || 0;
        zone = timezones[`UTC${utcNumber>=0?'+':''}${utcNumber}`];
        LineList = LineList.map(v => ({...v,timestamp: moment.tz(v.timestamp,zone).valueOf(), volume: v.vol }));
        LineList = LineList.filter(v => v.pkId != lineId);
        kLine = [...LineList,...kLine];
        kLine.sort((aLine,bLine) => (new Date(aLine.timestamp) > new Date(bLine.timestamp) ? 1 : -1) ); //按时间从小到大排序
    }
    return {zone,kLine};
}
//获取最新的数据
export async function getNewKLinefromGoods({goods,period,lineId}){
    let data = await baseInstance.get('v1/platform/selectAfterKLine',{
        params:{
            goods,
            period,
            lineId
        }
    });
    let LineList = data.data.data.lineData;
    let utcNumber = data.data.data.utc || 0;
    let zone = timezones[`UTC${utcNumber>=0?'+':''}${utcNumber}`];
    LineList = LineList.map(v => ({...v,timestamp:  moment.tz(v.timestamp,zone).valueOf(), volume: v.vol }));
    LineList.sort((aLine,bLine) => (new Date(aLine.timestamp) > new Date(bLine.timestamp) ? 1 : -1) ); //按时间从小到大排序
    return LineList;
}
//提交用于回测的数据
export async function commitTestBackList(goodsTradeArray){
    let data = await baseInstance.post('v1/platform/traderGoodsStrategy',{
        goodsTradeArray
    });
    return data.data.data;
}

//获取时间范围内的数据
export async function getKlineFromTimezone({goods,period,beginTime,endTime}){
    let data = await baseInstance.get('v1/platform/selectGoodsKLines',{
        params:{
            goods,
            period,
            beginTime,
            endTime,
        }
    });
    let LineList = data.data.data.lineData;
    let utcNumber = data.data.data.utc || 0;
    let zone = timezones[`UTC${utcNumber>=0?'+':''}${utcNumber}`];
    LineList = LineList.map(v => ({...v,timestamp: moment.tz(v.timestamp,zone).valueOf(), volume: v.vol }));
    LineList.sort((aLine,bLine) => (new Date(aLine.timestamp) > new Date(bLine.timestamp) ? 1 : -1) ); //按时间从小到大排序
    return ({lineList:LineList,timezone:zone});
}

//手动下单
export async function commitOrderList(opts){
    let data = await baseInstance.post('v1/platform/traderGoodsOrder',{
        ...opts
    });
    return data.data.data;
}